# -*- coding: utf-8 -*-
"""
Created on Thu Sep 15 23:05:47 2016

@author: zxj
"""
from PIL import Image,ImageDraw,ImageEnhance,ImageFont
import os

class Watermark(object):
    def __init__(self,image,text=None,color='white',position=(0,0),fontsize=12):
        """text:需要输入的水印文字
           image:原生图片的路径
           color:字体颜色
           position:水印文字的相对位置
           fontsize:字体大小
        """
        file=os.path.split(image)[1]
        self.name=os.path.splitext(file)[0]
        self.text=text
        self.font=ImageFont.truetype('simsun.ttc',fontsize)
        self.fontsize=fontsize
        self.size=(116,116)
        self.color=color
        self.mode='RGBA'
        self.worked_image=None
        self.position=position
        self.raw_image=Image.open(image)
    def produce(self):
        """创建一个带文字水印的图片
        """
        w_size,h_size=self.raw_image.size
        if w_size >self.size[0]:
            reduce_ratio=w_size/self.size[0]
            self.size=int(w_size/reduce_ratio),int(h_size/reduce_ratio)
        else:
            reduce_ratio=1
        print(self.raw_image.size)
        working_image=self.raw_image.convert('RGBA').resize(self.size) 
        draw=ImageDraw.ImageDraw(working_image,'RGBA')
        draw.setfont(self.font)
        if not self.text:
            self.text=''
        draw.text((self.size[0]*self.position[0],self.size[1]*self.position[1]),text=self.text,fill=self.color)
        self.worked_image=working_image
        return working_image
    def generate_as_html(self,filepath=None):
            img=self.produce()
            img_array=img.load()
            if not filepath:
                htmlfile=open('%s.html'%self.name,'w+')
            else:
                if os.path.isdir(os.path.split(filepath)[1]):
                    filepath=os.path.join(filepath,self.name)
                htmlfile=open('%s.html'%filepath,'w+')
            htmlfile.write("<html><head><style>table{margin:auto}td{width:6px;height:6px}</style></head><body><table>")
            w,h=img.size
            for pix_y in range(h):
                htmlfile.write('<tr>')
                for pix_x in range(w):
                    htmlfile.write("<td style='background:rgba(%s,%s,%s,%s);'></td>"%img_array[pix_x,pix_y])
                htmlfile.write('</tr>')
            htmlfile.write('</tabel></body></html>')   
            htmlfile.close()
    def generate_chars_as_html(self,filepath=None):
            img=self.produce()
            img=img.convert('1')
            img_array=img.load()
            if not filepath:
                htmlfile=open('%s.html'%self.name,'w+')
            else:
                if os.path.isdir(os.path.split(filepath)[1]):
                    filepath=os.path.join(filepath,self.name)
                htmlfile=open('%s.html'%filepath,'w+')
            htmlfile.write("<html><head><style>table{margin:auto;}td{width:6px;height:6px;cellpadding:0px;font-size:8px}</style></head><body><table>")
            w,h=img.size
            print(img.size[0])
            for pix_y in range(h):
                htmlfile.write('<tr>')
                for pix_x in range(w):
                    if img_array[pix_x,pix_y]==0:
                        htmlfile.write("<td>&hearts;</td>")
                    else:
                        htmlfile.write("<td>.</td>")
                htmlfile.write('</tr>')
            htmlfile.write('</tabel></body></html>')   
            htmlfile.close()
    def save(self,filepath):
        """
           filepath:保存文件的路径
        """
        self.worked_image.save(filepath)